package AcWing;

import java.util.Scanner;

/**
 * @author ZhuYi
 * @create 2025/3/20 17:40
 */
public class 背包01问题_2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int v = sc.nextInt();//背包容量
        int[] wgt = new int[n + 1];
        int[] val = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            wgt[i] = sc.nextInt();
            val[i] = sc.nextInt();
        }
        int[][] dp = new int[n + 1][v + 1];
        dp[0][0] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= v; j++) {
                if (j >= wgt[i]) {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - wgt[i]] + val[i]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        System.out.println(dp[n][v]);
    }
}
